#include "Log.h"

Log::Log(string textFilePath) {
	this->textFile = new TextFile(textFilePath,true);
	this->insertLogHeader();
}

Log::~Log() {
	if (this->textFile)
	{
		delete this->textFile;
	}
}

TextFile * Log::getTextFile() const{
	if (textFile == NULL)
		throw std::ios_base::failure("TextFile is not setted up");
	return this->textFile;
}

void Log::setTextFile(TextFile *textFile){
	this->textFile = textFile;
}

void Log::error(std::string message) {
	this->getTextFile()->escribir("[ERROR] " + message);
	this->getTextFile()->terminarLinea();
}

void Log::info(std::string message) {
	this->getTextFile()->escribir("[INFO]  " + message);
	this->getTextFile()->terminarLinea();
}

void Log::insertLogHeader(){
	//TODO generar archivo de constantes/configuracion
	string headerText = "ZUMA 'The Game'";
	string headerTimeText = "TIME: ";
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	
	this->getTextFile()->escribir(headerText);
	this->getTextFile()->terminarLinea();
	this->getTextFile()->escribir(headerTimeText);
	this->getTextFile()->tabular();
	this->getTextFile()->escribir(asctime (timeinfo));
	this->getTextFile()->terminarLinea();
}